
import { _decorator, Component, Node,Animation, Collider2D, Contact2DType } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = enemyManager
 * DateTime = Wed Aug 30 2023 03:00:51 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = enemyManager.ts
 * FileBasenameNoExtension = enemyManager
 * URL = db://assets/Script/enemyManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('enemyManager')
export class enemyManager extends Component {
    // [1]
    // dummy = '';
    private hp
    isHit:boolean
    private ani
    protected onLoad(): void {
        this.hp=0
        this.isHit=false
        this.ani=this.node.getComponent(Animation)

        this.ani.on(Animation.EventType.FINISHED, this.back, this)

    }
    back(e,data){
        this.hp--
        this.isHit=false

        if(this.hp==0){
            console.log("back")
            this.node.destroy()
        }
    }

    // [2]
    // @property
    // serializableDummy = 0;
    onBeginContact(self,other){
        if(other.node.group=='hero'){
            this.isHit=true
            this.ani.play('hurt')
        }
    }
    start () {

        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
